const Promise = require('bluebird');

/**
 * 操作订单
 */
exports.operate = function ({body, user_id}, {models: {Users, UserOrders}}) {
    let {opt, id} = body;
    if (!opt || !id) {
        return {success: false, messsage: '参数错误'};
    }
    if (opt == 'delete') {
        return UserOrders.findOne({where: {id: id, user_id: user_id}}).then(function (userOrder) {
            if (!userOrder) {
                return {success: false, messsage: '订单不存在'};
            }
            return UserOrders.update({status: 'Delete'}, {
                where: {
                    id: id,
                    user_id: user_id,
                    status: 'Pending'
                }
            }).then(function (result) {
                if (result[0]) {
                    return {success: true, messsage: '删除成功'};
                } else {
                    return {success: false, messsage: '删除失败'};
                }
            });
        });
    }
    return {success: false, messsage: '不支持操作'};
}